﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleApp6
{
    public class OrderService
    {
        private List<Order> orders = new List<Order>();

        // 添加订单
        public void AddOrder(Order order)
        {
            if (orders.Contains(order))
                throw new InvalidOperationException($"订单{order.OrderId}已存在");
            orders.Add(order);
        }

        // 删除订单
        public void RemoveOrder(string orderId)
        {
            var order = orders.FirstOrDefault(o => o.OrderId == orderId);
            if (order == null)
                throw new InvalidOperationException($"订单{orderId}不存在");
            orders.Remove(order);
        }

        // 修改订单
        public void UpdateOrder(Order newOrder)
        {
            var index = orders.FindIndex(o => o.OrderId == newOrder.OrderId);
            if (index < 0)
                throw new InvalidOperationException($"订单{newOrder.OrderId}不存在");
            orders[index] = newOrder;
        }

        // 查询所有订单
        public List<Order> GetAllOrders()
        {
            return orders.OrderBy(o => o.OrderId).ToList();
        }

        // 按订单号查询
        public List<Order> QueryByOrderId(string orderId)
        {
            return orders.Where(o => o.OrderId.Contains(orderId))
                        .OrderByDescending(o => o.TotalAmount)
                        .ToList();
        }

        // 按商品名称查询
        public List<Order> QueryByProductName(string productName)
        {
            return orders.Where(o => o.Details.Any(d => d.Product.Name.Contains(productName)))
                        .OrderByDescending(o => o.TotalAmount)
                        .ToList();
        }

        // 按客户查询
        public List<Order> QueryByCustomer(string customerName)
        {
            return orders.Where(o => o.Customer.Name.Contains(customerName))
                    .OrderByDescending(o => o.TotalAmount)
                    .ToList();
        }

        // 按金额范围查询
        public List<Order> QueryByAmountRange(double min, double max)
        {
            return orders.Where(o => o.TotalAmount >= min && o.TotalAmount <= max)
                        .OrderByDescending(o => o.TotalAmount)
                        .ToList();
        }

        // 排序方法
        public void Sort(Comparison<Order> comparison = null)
        {
            if (comparison == null)
                orders.Sort();
            else
                orders.Sort(comparison);
        }
        // 导出订单到XML文件
        public void Export(string filePath)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
                using (FileStream fs = new FileStream(filePath, FileMode.Create))
                {
                    serializer.Serialize(fs, orders);
                }
                Console.WriteLine($"订单数据已成功导出到 {filePath}");
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"导出订单失败: {ex.Message}");
            }
        }

        // 从XML文件导入订单
        public void Import(string filePath)
        {
            try
            {
                if (!File.Exists(filePath))
                    throw new FileNotFoundException($"文件 {filePath} 不存在");

                XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    List<Order> importedOrders = (List<Order>)serializer.Deserialize(fs);
                    orders.Clear();
                    orders.AddRange(importedOrders);
                }
                Console.WriteLine($"订单数据已成功从 {filePath} 导入");
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException($"导入订单失败: {ex.Message}");
            }
        }
    }
}
